package com.ecoolex.ying.space.admin.graphql;

import com.alibaba.fastjson.JSONArray;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.pojo.Sorting;
import com.ecoolex.framework.modularize.api.auth.admin.AdminMemberAuthority;
import com.ecoolex.framework.modularize.api.auth.annotation.AdminAuthoritativeRegister;
import com.ecoolex.framework.modularize.api.service.storage.StorageInfo;
import com.ecoolex.framework.modularize.api.service.storage.StorageService;
import com.ecoolex.framework.web.api.graphql2.annotation.*;
import com.ecoolex.ying.space.admin.core.AdminConstant;
import com.ecoolex.ying.space.entity.Feedback;
import com.ecoolex.ying.space.pojo.EditFeedbackDTO;
import com.ecoolex.ying.space.pojo.QueryFeedbackDTO;
import com.ecoolex.ying.space.ws.FeedbackService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.NonNull;

import java.util.List;
import java.util.stream.Collectors;

@GraphQL2Module(type = GraphQL2Module.Type.Admin, description = "反馈管理")
@AdminAuthoritativeRegister(parentKey = AdminConstant.AUTH_MODULE_KEY, key = FeedbackGraphql.AUTH, name = "反馈管理")
public class FeedbackGraphql {

    public static final String AUTH = AdminConstant.AUTH_MODULE_KEY + "-FEEDBACK";

    @Reference
    private FeedbackService feedbackService;

    @Autowired
    private StorageService storageService;

    @TypeField(type = Feedback.class, field = "images", description = "反馈图片")
    public List<StorageInfo> images(@Source Feedback source) {
        JSONArray array = JSONArray.parseArray(source.getImages());
        List<String> storageIdLsit = array.toJavaList(String.class);
        return storageIdLsit.stream().map(storageId -> storageService.get(storageId)).collect(Collectors.toList());
    }

    private static final String AUTH_GET = AUTH + "-GET";

    @QueryField(field = "feedback", description = "反馈详情")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_GET, name = "反馈详情")
    public Feedback get(@NonNull Integer id, AdminMemberAuthority user) {
        user.assertAuth(AUTH_GET);
        return feedbackService.getFeedbackById(id);
    }

    private static final String AUTH_QUERY = AUTH + "-QUERY";

    @QueryField(field = "feedbackList", description = "反馈列表")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_QUERY, name = "反馈列表")
    public Pagination<Feedback> query(QueryFeedbackDTO query, @NonNull Sorting[] sorts, @NonNull Page page, AdminMemberAuthority user) {
        user.assertAuth(AUTH_QUERY);
        return feedbackService.queryFeedback(query, sorts, page);
    }

    private static final String AUTH_EDIT = AUTH + "-EDIT";

    @MutationField(field = "editFeedback", description = "处理反馈")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_EDIT, name = "处理反馈")
    public Feedback editFeedback(@NonNull EditFeedbackDTO edit, AdminMemberAuthority user) {
        user.assertAuth(AUTH_EDIT);
        return feedbackService.updateFeedback(edit, user.userName());
    }

}
